package com.dayaokeji.rhythmschoolstudent.client.mine;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.MultiListFragment;
import com.dayaokeji.rhythmschoolstudent.client.home.course.CourseDetailActivity;
import com.dayaokeji.rhythmschoolstudent.client.home.course.adapter.CourseListAdapter;
import com.dayaokeji.rhythmschoolstudent.utils.UserUtils;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.api.CourseApi;
import com.dayaokeji.server_api.domain.Course;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import retrofit2.Call;

/**
 * Created by Gorden on 2017/10/16.
 * 课程列表
 */

public class CourseSearchListFragment extends MultiListFragment<Course,BaseViewHolder> {

    private boolean isQuery = false;
    private String keyword = "";

    public static CourseSearchListFragment newInstance() {
        
        Bundle args = new Bundle();
        
        CourseSearchListFragment fragment = new CourseSearchListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setAdapter(new CourseListAdapter());

        setOnListener();
    }


    private void setOnListener() {
        BaseQuickAdapter<Course, BaseViewHolder> adapter = getAdapter();
        if (adapter != null) {
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    Course course = (Course) adapter.getData().get(position);
                    Intent intent = new Intent(getActivity(), CourseDetailActivity.class);
                    intent.putExtra(CourseDetailActivity.EXTRA_COURSE_ENTITY,course);
                    startActivity(intent);
                }
            });
        }
    }

    public void queryKeyword(String keyword) {
        this.isQuery = true;
        this.keyword = keyword;

        onRefresh();
    }
    
    @Override
    protected Call createCall(int offset, int max) {
        if (isQuery) {
            return ApiUtils.getApi(CourseApi.class).queryKeyword(
                    UserUtils.getUserInfo().getId(),
                    UserUtils.getUserInfo().getUniversityId(),
                    1,
                    1,
                    keyword,
                    offset,
                    max
            );
        }else{
            return ApiUtils.getApi(CourseApi.class).history(
                    UserUtils.getUserInfo().getId(),
                    1,
                    UserUtils.getUserInfo().getUniversityId(),
                    1,
                    offset,
                    max
            );
        }
    }

    @Override
    protected Call createCall2(int offset, int max) {
        if (isQuery) {
            return ApiUtils.getApi(CourseApi.class).queryKeyword(
                    UserUtils.getUserInfo().getId(),
                    UserUtils.getUserInfo().getUniversityId(),
                    1,
                    2,
                    keyword,
                    offset,
                    max
            );
        }else{
            return ApiUtils.getApi(CourseApi.class).history(
                    UserUtils.getUserInfo().getId(),
                    1,
                    UserUtils.getUserInfo().getUniversityId(),
                    2,
                    offset,
                    max
            );
        }
    }
}
